<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "mall_video".
 *
 * @property int $id
 * @property int $cid 分类
 * @property string $title 标题
 * @property string $img 封面图
 * @property string $video_url 视频url
 * @property int $play_count 播放次数
 * @property string $author 视频作者
 * @property int $sort 排序
 * @property int $status 1正常 -1禁用
 * @property int $create_time 创建时间
 * @property int $update_time 修改时间
 */
class MallVideo extends \yii\db\ActiveRecord
{

    //定义场景
    const SCENARIO_UPDATE = 'update';
    const SCENARIO_UPLOAD = 'upload';
    const SCENARIO_CREATE = 'create';
    const SCENARIO_UPLOAD_POST = 'upload-post';

    public $file;

    public $files;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'mall_video';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['cid', 'title', 'img', 'video_url', 'create_time', 'update_time'], 'required'],
            [['cid', 'play_count', 'sort', 'status', 'create_time', 'update_time'], 'integer'],
            [['title'], 'string', 'max' => 30],
            [['img', 'video_url'], 'string', 'max' => 255],
            [['author'], 'string', 'max' => 20],
            [['file', 'files'], 'file'],
            [['file', 'files'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'cid' => '分类',
            'title' => '标题',
            'img' => '封面',
            'video_url' => '视频',
            'play_count' => '播放量',
            'author' => '作者',
            'sort' => '排序',
            'status' => '状态',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
        ];
    }

    /**
     * 枚举
     * @return array
     */
    public static function attributeValue()
    {
        return [
            'status' => [
                '-1' => '禁用',
                '1' => '正常',
            ],
        ];
    }

    /**
     * 状态
     * @return string
     */
    public function getStatusValue()
    {
        switch ($this->status)
        {
            case -1:
                $value = '禁用';
                break;
            case 1:
                $value = '正常';
                break;
            default:
                $value = '空';
                break;
        }
        return $value;
    }

    /**
     * 关联分类表
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(MallCategory::class, ['id' => 'cid']);
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_UPDATE] = ['cid', 'title', 'img', 'video_url', 'play_count', 'author', 'sort', 'status', 'create_time', 'update_time'];
        $scenarios[self::SCENARIO_UPLOAD] = ['file'];
        $scenarios[self::SCENARIO_UPLOAD_POST] = ['files'];
        $scenarios[self::SCENARIO_CREATE] = ['cid', 'title', 'img', 'video_url', 'play_count', 'author', 'sort', 'status', 'create_time', 'update_time'];
        return $scenarios;
    }
}
